import httpFetch from "share/httpFetch";
import { message } from "antd";
import config from "config";

const HotelServer = function (context) {
  context.setState({ loading: true });
  const realmId = config.realmIds[location.host] || config.realmId;
  return {
    // 获取酒店基础设置数据
    async getPlatformModelData(params) {
      try {
        const res = await httpFetch.get("/open/user/api/config/getHotelBaseConfig?realmId="+realmId, params, { source: "console" });
        return res;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存酒店基础设置数据
    async savePlatformModelData(params) {
      try {
        const response = await httpFetch.post("/open/user/api/config/saveHotelBaseConfig?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 获取酒店管控设置数据
    async getHotelControlData(params) {
      try {
        const response = await httpFetch.get(
          "/open/user/api/config/v2/getHotelControlConfig?realmId="+realmId,
          { ...params, ...{ configPage: true } },
          {
            source: "console",
          }
        );
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存酒店管控设置数据
    async saveHotelControlData(params) {
      try {
        const response = await httpFetch.post("/open/user/api/config/saveHotelControlConfig?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 获取酒店供应商
    async getHotelSupplierData(params) {
      try {
        const response = await httpFetch.get("/open/user/api/config/listHotelVendorSign?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 保存酒店供应商
    async saveHotelSupplierData(params) {
      try {
        const response = await httpFetch.post("/open/user/api/config/saveHotelVendorSignBatch?realmId="+realmId, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },

    // 根据ID获取人员组名字
    async getGroupNameById(id) {
      try {
        const response = await httpFetch.get(`${config.baseUrl}/api/user/groups/id?id=${id}`);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },
    // 添加常驻地
    async addResidence(params) {
      try {
        const response = await httpFetch.post(`${config.baseUrl}/open/user/api/config/saveHotelLocation?realmId=${realmId}`, params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({ loading: false });
      }
    },
  };
};

const dealWithErrorMes = (err) => {
  message.error(`${err.response.data.message}`);
};

export default HotelServer;
